package cn.com.gridinfo.sooc.core.course.note.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseDetailsInfo;
import cn.com.gridinfo.sooc.core.course.discuss.bean.Discuss;
import cn.com.gridinfo.sooc.core.course.info.bean.CourseProcess;
import cn.com.gridinfo.sooc.core.course.info.bean.Notice;
import cn.com.gridinfo.sooc.core.course.note.bean.Note;
import cn.com.gridinfo.sooc.core.course.note.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 课程 dao
 * Created by yingchunli on 2015/12/29.
 */
public class CourseDao extends SoocIDao {

    public String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    public String statusCode;//提示信息
    private List<Note> noteList;//笔记列表
    private String kcjj;//课程简介
    private CourseDetailsInfo courseInfo;//课程详情
    private List<Discuss> discussList;//讨论列表数据
    private List<Notice> noticeList;//通知列表
    private Discuss discuss;//讨论回复的列表
    private String isJudge = "";//课程是否可以评价
    private String hasCertificate;//是否有结业证书   0：没有 1：有结业证书
    private String isJoin;//是否加入课程   0：没有 1：加入了
    private List<CourseProcess> courseProcessList = null;//学习进度

    public CourseDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result == null) return;
        status = result.get("status").asText();
        statusCode = result.get("statusCode").asText();
        Log.d("result->" + result.toString());
        switch (requestCode) {
            //获取课程笔记
            case CodeConst.COURSE_GET_NOTELIST_FIRST:
            case CodeConst.COURSE_GET_NOTELIST_PULLUP:
            case CodeConst.COURSE_GET_NOTELIST_PUSHDOWN:
                noteList = JsonUtil.node2pojoList(result.get("data"), Note.class);
                Log.d("noteList.size()=" + noteList.size());
                break;
            case CodeConst.COURSE_GET_COURSE_SUMMARY://获取课程简介
                kcjj = result.get("data").get("kcjj").asText();
                Log.d("课程简介kcjj->" + kcjj);
                break;
            case CodeConst.COURSE_GET_COURSE_NOTICE://获取课程通知
                noticeList = JsonUtil.node2pojoList(result.get("data"), Notice.class);
                Log.d("noticeList.size=" + noticeList.size());
                break;
            case CodeConst.COURSE_GET_COURSE_INFO://获取课程详情
                courseInfo = JsonUtil.node2pojo(result.get("data"), CourseDetailsInfo.class);
                Log.d("老师名称->" + courseInfo.getUname());
                break;

            //获取讨论列表
            case CodeConst.COURSE_GET_COURSE_DISCUSS_FIRST:
            case CodeConst.COURSE_GET_COURSE_DISCUSS_PULLUP:
            case CodeConst.COURSE_GET_COURSE_DISCUSS_PUSHDOWN:
                discussList = JsonUtil.node2pojoList(result.get("data"), Discuss.class);
                Log.d("discussList.size=" + discussList.size());
                break;

            //获取讨论详情
            case CodeConst.COURSE_GET_COURSE_DISCUSSINFO_FIRST:
            case CodeConst.COURSE_GET_COURSE_DISCUSSINFO_PULLUP:
            case CodeConst.COURSE_GET_COURSE_DISCUSSINFO_PUSHDOWN:
                discuss = JsonUtil.node2pojo(result.get("data"), Discuss.class);
                Log.d("hfts->" + discuss.getHfts());
                break;

            //获取课程评价是否可以评价
            case CodeConst.COURSE_GET_COURSE_JUDGE:
                isJudge = result.get("data").get("isJudge").asText();
                break;

            //课程结课标准
            case CodeConst.COURSE_STAND:
                hasCertificate = result.get("data").get("hasCertificate").asText();
                isJoin = result.get("data").get("isJoin").asText();
                //结课标准
                courseProcessList = JsonUtil.node2pojoList(result.get("data").get("target"), CourseProcess.class);
                break;
        }
    }

    /**
     * 获取课程笔记
     *
     * @param kcid        课程ID
     * @param mlid        章ID    可以不传
     * @param page        页码
     * @param num         每页返回数组个数。
     * @param requestCode 请求码
     */
    public void getNote(String kcid, String mlid, int page, int num, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "note", true);
        Log.d("获取课程笔记：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("mlid", mlid);
        params.add("page", page + "");
        params.add("num", num + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 获取系统时间
     */
    public void getSystemTime() {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "time", true);
        Log.d("获取系统时间：url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, 0);
    }

    /**
     * 笔记 点赞
     *
     * @param bjid        笔记id
     * @param dzzt        点赞状态  1:点赞  0:取消点赞
     * @param requestCode 请求码
     */
    public void dianZan(String bjid, String dzzt, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "dianZan", true);
        Log.d("获取系统时间：url->" + url);
        RequestParams params = new RequestParams();
        params.add("bjid", bjid);
        params.add("dzzt", dzzt);
        postRequest(url, params, requestCode);
    }

    /**
     * 写笔记
     *
     * @param kcid        课程ID
     * @param mlid        章ID
     * @param nrid        内容ID
     * @param pmlid       节id
     * @param sfgk        是否公开  0：不公开   1：公开（默认为公开）
     * @param bjnr        笔记内容
     * @param requestCode 请求码
     */
    public void writeNote(String kcid, String mlid, String nrid, String pmlid, String sfgk, String bjnr, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "writeNote", true);
        Log.d("写笔记：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("mlid", mlid);
        params.add("nrid", nrid);
        params.add("pmlid", pmlid);
        params.add("sfgk", sfgk);
        params.add("bjnr", bjnr);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程简介
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getCourseSummary(String kcid, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "intro", true);
        Log.d("获取简介：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程通知
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getCourseNotice(String kcid, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "notice", true);
        Log.d("获取通知：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程详情
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getCourseInfo(String kcid, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "info", true);
        Log.d("获取详情：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程讨论列表
     *
     * @param kcid        课程id
     * @param pmlid       章 id
     * @param mlid        节id
     * @param pageNo      当前页码
     * @param pageSize    每页显示的条数
     * @param requestCode 请求码
     */
    public void getCourseDiscuss(String kcid, String pmlid, String mlid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "discusslist", true);
        Log.d("获取讨论列表：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("pmlid", pmlid);
        params.add("mlid", mlid);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 获取讨论详情
     *
     * @param tlid        讨论id
     * @param pageNo      当前页码
     * @param pageSize    每页显示的条数
     * @param requestCode 请求码
     */
    public void getCourseDiscussInfo(String tlid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "discussInfo", true);
        Log.d("获取讨论详情列表：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("tlid", tlid);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 发布讨论
     *
     * @param kcid        课程id
     * @param pmlid       章ID
     * @param mlid        节ID
     * @param nrid        素材id
     * @param tlbt        评论内容
     * @param requestCode 请求码
     */
    public void sendDiscuss(String kcid, String pmlid, String mlid, String nrid, String tlbt, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "addDiscuss", true);
        Log.d("获取讨论列表：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("pmlid", pmlid);
        params.add("mlid", mlid);
        params.add("nrid", nrid);
        params.add("tlbt", tlbt);
        postRequest(url, params, requestCode);
    }

    /**
     * 回复讨论
     *
     * @param tlid        讨论id
     * @param hfnr        回复内容
     * @param requestCode 请求码
     */
    public void hfDiscuss(String tlid, String id, String hfnr, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "replyDiscuss", true);
        Log.d("获取讨论列表：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("tlid", tlid);
        params.add("hfnr", hfnr);
        postRequest(url, params, requestCode);
    }


    /**
     * 提交课程评价
     *
     * @param kcid        课程id
     * @param count       评价的级数
     * @param requestCode 请求码
     */
    public void judge(String kcid, String count, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "judge", true);
        Log.d("课程评价：url->" + url.toString());
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        if (count != null)
            params.add("count", count);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程结课标准
     *
     * @param kcid 课程id
     */
    public void courseStand(String kcid) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Course.toString(), "getStudyDetail", true);
//        RequestParams params = new RequestParams();
//        params.add("kcid", kcid);
//        postRequest(url, params, CodeConst.COURSE_STAND);
        Map<String,String> map=new HashMap<>();
        map.put("kcid", kcid);
        getRequestForCode(url,map,CodeConst.COURSE_STAND);
    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public List<Note> getNoteList() {
        return noteList;
    }

    public String getKcjj() {
        return kcjj;
    }

    public CourseDetailsInfo getCourseInfo() {
        return courseInfo;
    }

    public List<Discuss> getDiscussList() {
        return discussList;
    }

    public List<Notice> getNoticeList() {
        return noticeList;
    }

    public Discuss getDiscuss() {
        return discuss;
    }

    public String getIsJudge() {
        return isJudge;
    }

    public String getHasCertificate() {
        return hasCertificate;
    }

    public String getIsJoin() {
        return isJoin;
    }

    public List<CourseProcess> getCourseProcessList() {
        return courseProcessList;
    }

}
